const db = require("../models");
const Tech = db.tech;
const Depend = db.depend;
const Learning_route = db.learning_route;
const Daily_task = db.daily_task;

const { Op } = require("sequelize");
var moment = require("moment");
const { learning_route } = require("../models");

const learn = require("../controllers/learning_route.controller");

exports.listAllTech = async (req, res) => {
  await learn.checkReadyForUser(req.query.user_mail);

  try {
    let candidate = [];
    let already_added = [];
    let ret_val = [];
    await Learning_route.findAll({
      where: {
        user_mail: req.query.user_mail,
        ready: true,
        process: { [Op.lt]: 1 },
      },
      raw: true,
    }).then((data) => {
      candidate = data;
    });

    // console.log(moment().format('YYYY-MM-DD'))
    await Daily_task.findAll({
      where: {
        date: moment().format("YYYY-MM-DD"),
      },
      raw: true,
    }).then((data) => {
      already_added = data;
    });

    for (let i = 0; i < candidate.length; i++) {
      let m_exist = false;
      for (let j = 0; j < already_added.length; j++) {
        if (candidate[i].tech_id === already_added[j].tech_id) {
          m_exist = true;
          break;
        }
      }

      if (m_exist === false) {
        ret_val.push(candidate[i]);
      }
    }

    // get tech name
    for (let i = 0; i < ret_val.length; i++) {
      await Tech.findOne({
        where: {
          tech_id: ret_val[i].tech_id,
        },
        raw: true,
      }).then((data) => {
        ret_val[i].tech_name = data.tech_name;
      });
    }

    res.setHeader("Access-Control-Allow-Origin", "*");
    res.send(ret_val);
  } catch (err) {
    res.status(500).send({
      message: err.message || "未查询到该用户该日的学习计划",
    });
  }
};

//获取某一日学习计划
exports.getOneStudySchedule = async (req, res) => {
  try {
    let ret_data = [];
    await Daily_task.findAll({
      where: { user_mail: req.query.user_mail, date: req.query.date },
    }).then((data) => {
      ret_data = data;
    });

    // get tech name
    for (let i = 0; i < ret_data.length; i++) {
      await Tech.findOne({
        where: {
          tech_id: ret_data[i].tech_id,
        },
        raw: true,
      }).then((data) => {
        ret_data[i].dataValues.tech_name = data.tech_name;
      });
    }

    res.setHeader("Access-Control-Allow-Origin", "*");
    res.send(ret_data);
  } catch (err) {
    res.status(500).send({
      message: err.message || "未查询到该用户该日的学习计划",
    });
  }
};

//获取今日学习计划
exports.getStudySchedule = async (req, res) => {
  try {
    let ret_data = [];
    await Daily_task.findAll({
      where: {
        user_mail: req.query.user_mail,
        date: moment().format("YYYY-MM-DD"),
      },
    }).then((data) => {
      ret_data = data;
    });

    // get tech name
    for (let i = 0; i < ret_data.length; i++) {
      await Tech.findOne({
        where: {
          tech_id: ret_data[i].tech_id,
        },
        raw: true,
      }).then((data) => {
        ret_data[i].dataValues.tech_name = data.tech_name;
      });
    }

    // get process
    for (let i = 0; i < ret_data.length; i++) {
      await Learning_route.findOne({
        where: {
          tech_id: ret_data[i].tech_id,
          user_mail: req.query.user_mail
        },
        raw: true,
      }).then((data) => {
        ret_data[i].dataValues.process = data.process;
      });
    }

    res.setHeader("Access-Control-Allow-Origin", "*");
    res.send(ret_data);
  } catch (err) {
    res.status(500).send({
      message: err.message || "未查询到该用户今日的学习计划",
    });
  }
};

//添加技术到学习计划
exports.insertStudySchedule = async (req, res) => {
  try {
    var tid;
    await Tech.findOne({
      where: {
        tech_name: req.query.tech_name,
      },
      raw: true,
    }).then((data) => {
      tid = data.tech_id;
    });
    Daily_task.create({
      user_mail: req.query.user_mail,
      tech_id: tid,
      date: moment().format("YYYY-MM-DD"),
    }).then((data) => {
      res.setHeader("Access-Control-Allow-Origin", "*");
      res.send(data);
    });
  } catch (err) {
    res.status(500).send({
      message: err.message || "添加技术到学习计划失败",
    });
  }
};

//删除技术到学习计划
exports.deleteStudySchedule = async (req, res) => {
  try {
    var tid;
    await Tech.findOne({
      where: {
        tech_name: req.query.tech_name,
      },
      raw: true,
    }).then((data) => {
      tid = data.tech_id;
    });
    Daily_task.destroy({
      where: {
        user_mail: req.query.user_mail,
        tech_id: tid,
        date: moment().format("YYYY-MM-DD"),
      },
    })
      res.setHeader("Access-Control-Allow-Origin", "*");
      res.send("delete success!");
  } catch (err) {
    res.status(500).send({
      message: err.message || "删除技术到学习计划失败",
    });
  }
};
